package cn.net.yto.school.web.index;

import cn.net.yto.school.dao.dto.ClazzStudentCountStsDto;
import cn.net.yto.school.service.ClazzService;
import cn.net.yto.school.service.StudentService;
import com.github.abel533.echarts.AxisPointer;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.Tooltip;
import com.github.abel533.echarts.code.PointerType;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.force.Category;
import javafx.scene.chart.CategoryAxis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("queryIndex")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) {

        // 获取到系统中所有的班级和学生数量
        Long clazzCount = this.clazzService.countClazzes();
        Long stuCount = this.studentService.countStus();

        // 获取系统中每个班的学生数量

        List<ClazzStudentCountStsDto> countClazzStudent = this.clazzService.countClazzStudent();


        model.addAttribute("clazzCount", clazzCount);
        model.addAttribute("stuCount", stuCount);
        model.addAttribute("countClazzStudent", countClazzStudent);

        return "index_count";
    }

    @RequestMapping("showDiagram")
    @ResponseBody
    public Option showClazzStu(HttpServletRequest request, HttpServletResponse response, Model model) {
        // 获取到数据对象
        List<ClazzStudentCountStsDto> countClazzStudent = this.clazzService.countClazzStudent();
        //创建option对象
        Option option = new Option();
        option.title("各班人数统计图");
        option.color(new Color(0, 172, 172));

        Tooltip tooltip = new Tooltip();
        tooltip.setTrigger(Trigger.axis);
        AxisPointer axisPointer = new AxisPointer();
        axisPointer.setType(PointerType.shadow);
        tooltip.setAxisPointer(axisPointer);
        option.tooltip(tooltip);

        //此处少一个grid

        Category category = new Category();
        CategoryAxis categoryAxis = new CategoryAxis();
//        categoryAxis.set

        Bar bar = new Bar();
        bar.setName("班级人数");
        bar.setType(SeriesType.bar);
        bar.setBarWidth(60);
        List<Object> barData = new ArrayList<>();
        for(ClazzStudentCountStsDto c : countClazzStudent){
            categoryAxis.setUserData(c.getClsName());
            barData.add(c.getCountNum());
        }
        bar.setData(barData);


        return option;
    }
}
